﻿using System;

namespace TempCshape
{
    public class Solution1_1
    {
        public int PivotIndex(int[] nums)
        {
            int len = nums.Length;
            int total = 0, leftTotal = 0;
            //首先求出所有数的和
            for (int i = 0; i < len; ++i)
            {
                total += nums[i];
            }
            //开始从左往右移动
            for (int i = 0; i < len; ++i)
            {
                if (leftTotal == total - nums[i])
                {
                    return i;
                }
                leftTotal += nums[i];
                total -= nums[i];
            }
            return -1;
        }
    }
    public class Solution1_2
    {
        public int SearchInsert(int[] nums, int target)
        {
            int numsLen = nums.Length;
            int res = -1, i;
            for (i = 0; i < numsLen; ++i)
            {
                if (target <= nums[i] && res == -1)
                {
                    res = i;
                }
                if (target == nums[i])
                {
                    res = i;
                    break;
                }
            }
            if (res == -1)
            {
                return i;
            }
            return res;
        }
    }
    public class Solution2_3
    {
        public int[] FindDiagonalOrder(int[][] matrix)
        {
            //vector<int> ress;
            int[] ress = new int[0];
            //if(matrix.empty()) return ress;
            if (matrix.GetLength(0) == 0) return ress;
            //int xLen = matrix.size(), yLen = matrix[0].size();
            int xLen = matrix.Length, yLen = matrix[0].Length;
            //vector<int> res(xLen*yLen);
            int[] res = new int[xLen * yLen];
            if (xLen == 1 && yLen == 1)
            {
                res[0] = matrix[0][0];
                return res;
            }
            int i = 0;
            int x = 0, y = 0, mov = 0;
            int flag = 1;//转向动态
            for (; i < (xLen * yLen); i++)
            {
                //赋值---------------
                res[i] = matrix[x][y];
                Console.WriteLine(matrix[x][y]);
                //一直向右
                if (x == 0 && xLen == 1)
                {
                    mov = 2;
                }
                //一直向下
                else if (y == 0 && yLen == 1)
                {
                    mov = 1;
                }
                //角落向下
                else if (x == 0 && y == yLen - 1 && yLen % 2 == 1)
                {
                    mov = 1;
                    flag = 1;
                }
                //角落向右
                else if (x == xLen - 1 && y == 0 && xLen % 2 == 0)
                {
                    mov = 2;
                    flag = 1;
                }
                //开始向右
                else if (x == 0 && y == 0)
                {
                    mov = 2;
                    flag = 1;
                }
                //边界向右上
                else if ((x == xLen - 1 || y == 0) && flag == 1)
                {
                    mov = 3;
                    flag = 2;
                }
                //边界向左下
                else if ((x == 0 || y == yLen - 1) && flag == 1)
                {
                    mov = 4;
                    flag = 3;
                }
                //角落向左下
                else if (x == 0 && y == yLen - 1)
                {
                    mov = 4;
                    flag = 3;
                }
                //角落向右上
                else if (x == xLen - 1 && y == 0)
                {
                    mov = 3;
                    flag = 2;
                }
                //向右
                else if (x == 0 || x == xLen - 1)
                {
                    mov = 2;
                    flag = 1;
                }
                //向下
                else if (y == 0 || y == yLen - 1)
                {
                    mov = 1;
                    flag = 1;
                }
                //继承上一个移位状态
                //右上
                else if (flag == 2)
                {
                    mov = 3;
                }
                else if (flag == 3)
                {
                    mov = 4;
                }
                //移位判断-----------
                //向右
                if (mov == 2)
                {
                    x += 0;
                    y += 1;
                }
                //向下
                else if (mov == 1)
                {
                    x += 1;
                    y += 0;
                }
                //右上
                else if (mov == 3)
                {
                    x += -1;
                    y += 1;
                }
                //左下
                else
                {
                    x += 1;
                    y += -1;
                }
            }
            return res;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            //输入数据定义
            int[][] matrix = new int[3][];
            matrix[0] = new int[3] { 1, 2, 3 };
            matrix[1] = new int[3] { 4, 5, 6 };
            matrix[2] = new int[3] { 7, 8, 9 };
            //创建对象调用方法
            Solution2_3 s2_3 = new Solution2_3();
            s2_3.FindDiagonalOrder(matrix);
            Console.ReadLine();
        }
    }
}
